
#include "sql_interface_stub.h"
#include "../LmpToDb/lmp_to_db_sql_query.h"

#include <String.h>
#include <iostream>
#include "../LmpToDb/sql_interface.h"
#include "../sqlite/sqlite3Impl.h"
using namespace std;
void DataBase::InitDB (char* path)
{
    char query[5120] = {0,};
    pSQL = new Sqlite3Impl();
    pSQL->open(path);

    sprintf(query, CREATE_TABLE_QUERY, LMPINFO_TABLE_NAME, LMPINFO_COLUMNS);
    pSQL->query(query);
}

int DataBase::PushInfoStruct(INFO_LMP* sInfoLmp)
{
    char query[5120] = {0,};
    int FlagInfoInt;
    if (sInfoLmp->FlagInfo == true)
        FlagInfoInt = 1;
    else
        FlagInfoInt = 0;

    sprintf(query, INSERT_INTO_TABLE_QUERY, LMPINFO_TABLE_NAME);


    if(pSQL)
    {
        void* stat = pSQL->prepare(query);
        if(stat)
        {
           int i = 1;
           pSQL->bind(stat,i, FlagInfoInt);             i++;
           pSQL->bind(stat,i, sInfoLmp->NameFile);      i++;
           std::string str;
           str+=sInfoLmp->Regim;
           pSQL->bind(stat,i, (char*)str.c_str());      i++;
           pSQL->bind(stat,i, sInfoLmp->XminRastr);     i++;
           pSQL->bind(stat,i, sInfoLmp->XmaxRastr);     i++;
           pSQL->bind(stat,i, sInfoLmp->YminRastr);     i++;
           pSQL->bind(stat,i, sInfoLmp->YmaxRastr);     i++;
           pSQL->bind(stat,i, sInfoLmp->XminInf);       i++;
           pSQL->bind(stat,i, sInfoLmp->XmaxInf);       i++;
           pSQL->bind(stat,i, sInfoLmp->YminInf);       i++;
           pSQL->bind(stat,i, sInfoLmp->YmaxInf);       i++;
           pSQL->bind(stat,i, sInfoLmp->Hx_Pix);        i++;
           pSQL->bind(stat,i, sInfoLmp->Hy_Pix);        i++;
           pSQL->bind(stat,i, sInfoLmp->CountPage);     i++;
           pSQL->bind(stat,i, sInfoLmp->LengthLastPage);i++;
           pSQL->bind(stat,i, sInfoLmp->LengthPage);    i++;
           pSQL->bind(stat,i, sInfoLmp->CoObjMax);      i++;
           pSQL->bind(stat,i, sInfoLmp->MaxLengthObject);i++;
           pSQL->bind(stat,i, sInfoLmp->Null_X);        i++;
           pSQL->bind(stat,i, sInfoLmp->Null_Y);        i++;
           pSQL->bind(stat,i, (void*)(sInfoLmp->PRK),sizeof(long)*8);i++;
           pSQL->bind(stat,i, (void*)(sInfoLmp->RK),sizeof(long)*8); i++;
           pSQL->bind(stat,i, (void*)(sInfoLmp->TK),sizeof(double)*8); i++;
        }

    	pSQL->step(stat);


        vector<vector<string> > val = pSQL->query(GET_PASPORT_TABLE_NAME);
        string s = val[0][0];

        sprintf(query, CREATE_TABLE_QUERY, GetPassportNmae(s).c_str(), PASPORT_COLUMNS);
        pSQL->query(query);

    }

    return 1;
}

void DataBase::StartTransaction()
{
    pSQL->exec("BEGIN;");
}

void DataBase::EndTransaction()
{
    pSQL->exec("COMMIT;");
}
std::string DataBase::GetPassportNmae(std::string s){
    return "passport" + s;
}
int DataBase::PushPassportAndMetrics(Pasport* psPassport, POINT_LMP* pMetric,int sizeOfMetrics)
{
     char query[5120] = {0,};
     string PASPORTS_TABLE_NAME;
     vector<vector<string> > val = pSQL->query(GET_PASPORT_TABLE_NAME);
     string s = val[0][0];
     PASPORTS_TABLE_NAME = GetPassportNmae(s);

    sprintf(query, INSERT_INTO_TABLE_QUERY_PASPORTS, PASPORTS_TABLE_NAME);

    if(pSQL)
    {
        void* stat = pSQL->prepare(query);
        if(stat)
        {
           int i = 1;
           pSQL->bind(stat,i, psPassport->Free1);       i++;
           pSQL->bind(stat,i, psPassport->NumBlock);    i++;
           pSQL->bind(stat,i, psPassport->OffInBlock);      i++;
           pSQL->bind(stat,i, psPassport->LenMetric);     i++;
           pSQL->bind(stat,i, psPassport->NumObject);     i++;
           pSQL->bind(stat,i, psPassport->Xmin);     i++;
           pSQL->bind(stat,i, psPassport->Xmin);     i++;
           pSQL->bind(stat,i, psPassport->Ymin);       i++;
           pSQL->bind(stat,i, psPassport->Ymax);       i++;
           pSQL->bind(stat,i, psPassport->LenLine);       i++;
           pSQL->bind(stat,i, psPassport->Free2);       i++;
           pSQL->bind(stat,i, psPassport->Type);        i++;
           pSQL->bind(stat,i, psPassport->Free3);        i++;
           pSQL->bind(stat,i, psPassport->Free4);i++;
           pSQL->bind(stat,i, psPassport->CountPoint);    i++;
           pSQL->bind(stat,i, psPassport->Width);i++;
           pSQL->bind(stat,i, (void*)(pMetric),sizeOfMetrics*sizeof(long)*2);i++;
        }
        pSQL->step(stat);
    }
    return 1;
}

